package com.atguigu.lease.web.app.service.impl;


import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.RoomInfoMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.model.entity.*;

import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private FeeValueService feeValueService;
    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Override
    public RoomDetailVo getRoomById(Long id) {
        RoomInfo roomInfo = getById(id);
        ApartmentItemVo apartmentItemVo=apartmentInfoService.queryVoById(roomInfo.getApartmentId());
        List<GraphVo> graphVos=graphInfoService.queryVoByRoomId(id);
        List<AttrValueVo> attrValueVos=attrValueService.listVoByRoomId(id);
        List<FacilityInfo> facilityInfos=facilityInfoService.listInfoByRoomId(id);
        List<LabelInfo> labelInfos=labelInfoService.listInfoByRoomId(id);
        List<PaymentType> paymentTypes=paymentTypeService.listInfoByRoomId(id);
        List<FeeValueVo> feeValueVos=feeValueService.listVoByRoomId(id);
        List<LeaseTerm> leaseTerms=leaseTermService.listByRoomId(id);
        Boolean isDelete=roomInfo.getIsDeleted()==1;
        LambdaQueryWrapper<LeaseAgreement> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(LeaseAgreement::getRoomId,id).in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.RENEWING);
        long count = leaseAgreementService.count(wrapper);
        Boolean isCheckIn=count>0;
        RoomDetailVo vo = new RoomDetailVo();
        vo.setApartmentItemVo(apartmentItemVo);
        vo.setFacilityInfoList(facilityInfos);
        vo.setIsDelete(isDelete);
        vo.setGraphVoList(graphVos);
        vo.setLabelInfoList(labelInfos);
        vo.setAttrValueVoList(attrValueVos);
        vo.setIsCheckIn(isCheckIn);
        vo.setFeeValueVoList(feeValueVos);
        vo.setLeaseTermList(leaseTerms);
        vo.setPaymentTypeList(paymentTypes);
        BeanUtils.copyProperties(roomInfo,vo);
        return vo;
    }

    @Override
    public void listRoom(IPage<RoomItemVo> page, RoomQueryVo queryVo,Long id) {
        page=roomInfoMapper.selectListRoom(page,queryVo,id);
    }
}




